<?php
require_once ('..'.DIRECTORY_SEPARATOR.'initialize.php');
include_once(Utils::getPath(array('shared', 'template.php')));
include_once(Utils::getPath(array('repositories', 'challengeRepository.php')));

$template = new Template('application');
$template->initialize();

$challengeRep = new ChallengeRepository();
$challenges = $challengeRep->getChallengeListForUser($_SESSION['authenticatedId']);
$categories = $challengeRep->getAllCategories('Id', null, 0, 0);

$num_challenges = count($challenges);
$num_categories = count($categories);

$current_challenge_index = 0;
?>
<script type="text/javascript" src="<?php echo Utils::getUrl('scripts/challenge.js') ?>"></script>
<h2>Challenges</h2>
<div class = "list">
    <div class = "challenge-header">
        <table>
            <tr>
                <td class = "header-title">Challenge title</td>
                <td class = "header-done-by">Solved by</td>
                <td class = "header-done">Done?</td>
            </tr>
        </table>
    </div>
    <?php
    for ($i = 0; $i < $num_categories; $i++) {
        ?>
        <div class = "challenge-category-separator"></div>
        <div class = "challenge-category" id = "category-<?php echo $categories[$i]->getId(); ?>" onclick = "toggleCategoryView(this);">
            <div class ="toggle-category" id="check-<?php echo $categories[$i]->getId(); ?>">-</div>    
            <span><?php echo $categories[$i]->getName(); ?></span> - <?php echo $categories[$i]->getDescription(); ?>
        </div>
        <div class = "challenge-group" id = "group-<?php echo $categories[$i]->getId(); ?>">
            <table>
                <?php
                while ($current_challenge_index < $num_challenges) {
                    $current_challenge = $challenges[$current_challenge_index];
                    if (!isset($current_challenge['solutions']))
                        $current_challenge['solutions'] = 0;

                    if ($current_challenge['category'] == $categories[$i]->getId()) {
                        $urlChallenge = 'view.php?id=' . $current_challenge['id'];
                        ?>
                        <tr class="list-item" onclick="location.href='<?php echo $urlChallenge; ?>'">
                            <td class="challenge-title"><a href="<?php echo $urlChallenge; ?>" ><?php echo $current_challenge['title']; ?></a> [made by <a href="<?php echo Utils::getUrl('user/profile.php?id=' . $current_challenge['authorid']) ?>" ><?php echo $current_challenge['author'] ?></a>]</td>
                            <td class="done-by"><?php echo $current_challenge['solutions']; ?></td>
            <?php
            if (!isset($current_challenge['usersolutions'])) {
                ?>
                                <td class="not-done">Not done</td>
                                <?php
                            } else {
                                ?>
                                <td class="done">Done</td>
                                <?php
                            }
                            ?>
                        </tr>
                            <?php
                            $current_challenge_index++;
                        } else {
                            break;
                        }
                    }
                    ?>
            </table>
        </div>
                <?php
            }
            ?>
</div>
            <?php
            $template->createPage('Challenges');
            ?>